package com.smallrig.mall.template.controller;

import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;

@RestController
public class SeckillController {

    private String salt = "safhjklluoi8998htYIUyuhu";

    @GetMapping("/seckill/{id}")
    public String getSeckillUrl(@PathVariable Long id) {
        String originStr = salt + id;
        String md5Str = DigestUtils.md5DigestAsHex(originStr.getBytes(StandardCharsets.UTF_8));
        return md5Str;
    }

    @PostMapping("/seckill/{id}")
    public String seckill(@PathVariable Long id, @RequestParam String token) {
        String originStr = salt + id;
        String md5Str = DigestUtils.md5DigestAsHex(originStr.getBytes(StandardCharsets.UTF_8));
        if (!md5Str.equals(token)) {
            return "Token不合法，秒杀失败！";
        }
        // 秒杀逻辑代码
        return "秒杀成功！";
    }
}
